package InventoryAssetReceipt

import (
	"ToolLibray/jwtx"
	"context"
	"property_api/utils/result"
	"property_service/client/inventory_service"

	"property_api/internal/svc"
	"property_api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type PagesLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewPagesLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PagesLogic {
	return &PagesLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

// Pages 查询资产入库单的分页逻辑
func (l *PagesLogic) Pages(req *types.InventoryAssetReceiptPageReq) (resp *types.PageRes, err error) {
	// 设置租户ID，从上下文中获取
	tenantID := jwtx.GetUserTenantId64(l.ctx)

	// 构建查询条件对象
	where := &inventory_service.InventoryAssetReceipt{
		TenantID:               tenantID,                   // 租户ID
		ID:                     req.ID,                     // 记录ID
		ReceiptCode:            req.ReceiptCode,            // 入库单号
		ReceiptDate:            req.ReceiptDate,            // 入库日期
		EquipmentSource:        req.EquipmentSource,        // 设备来源
		Receiver:               req.Receiver,               // 验收人
		ReceiverID:             req.ReceiverID,             // 验收人ID
		Warehouse:              req.Warehouse,              // 库房
		Supplier:               req.Supplier,               // 供应商
		SupplierID:             req.SupplierID,             // 供应商ID
		Purchaser:              req.Purchaser,              // 采购员
		PurchaserID:            req.PurchaserID,            // 采购员ID
		ContractNumber:         req.ContractNumber,         // 合同编号
		ContractID:             req.ContractID,             // 合同ID
		InvoiceNumber:          req.InvoiceNumber,          // 发票号码
		InvoiceDate:            req.InvoiceDate,            // 发票日期
		DepreciationMethod:     req.DepreciationMethod,     // 折旧方法
		DepreciationMethodCode: req.DepreciationMethodCode, // 折旧方法
		AssetUse:               req.AssetUse,               // 资产用途
		ReceiptMethod:          req.ReceiptMethod,          // 入库方式
		InstallationNumber:     req.InstallationNumber,     // 安装单号
		StatusID:               req.StatusID,               // 状态ID
		AssetUseCode:           req.AssetUseCode,           //资产用途Code
		ReceiptMethodCode:      req.ReceiptMethodCode,      //入库方式Code
		PurchaseID:             req.PurchaseID,             // 采购申请单ID
		PurchaseCode:           req.PurchaseCode,           // 采购編碼
		StatusName:             req.StatusName,             // 采购状态
		StockID:                req.StockID,                // 仓库ID
	}

	// 调用服务层方法，查询资产入库单记录的分页数据
	// 服务层方法 InventoryAssetReceiptPage 会处理实际的数据库查询操作
	data, err := l.svcCtx.InventoryService.InventoryAssetReceiptPage(l.ctx, &inventory_service.InventoryAssetReceiptPageReq{
		Page:  req.Page,  // 当前页码
		Limit: req.Limit, // 每页条数
		Where: where,     // 查询条件
	})

	// 返回分页数据的响应
	return result.SucPage(data, types.InventoryAssetReceipt{}).PageEd()
}
